﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Cooksta_Data;

namespace Cooksta_Inventory_Manager
{
    public static class InventoryActions
    {
        private static string _connectionString = "";
        public static string ConnectionString { get { return _connectionString; } set { _connectionString = value; } }

        public static int Insert_Inventroy_Item(InventoryItem item)
        {
            try
            {
                return Convert.ToInt32(DataAccess.Insert_Data(_connectionString,"Insert_Item",CommandType.StoredProcedure,
                                  new SqlParameter[] {new SqlParameter("@itemDescription",item.Description),
                                                      new SqlParameter("@itemCost",item.Cost),
                                                      new SqlParameter("@itemPrice",item.Price),
                                                      new SqlParameter("@pictureLocation",item.PictureLocation)}));
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message,ex.InnerException);
            }
        }

        public static void Update_Inventroy_Item(InventoryItem item)
        {
            try
            {
                DataAccess.Insert_Data(_connectionString, "Insert_Item", CommandType.StoredProcedure,
                                  new SqlParameter[] {new SqlParameter("@itemID",item.ItemID),
                                                      new SqlParameter("@itemDescription",item.Description),
                                                      new SqlParameter("@itemCost",item.Cost),
                                                      new SqlParameter("@itemPrice",item.Price),
                                                      new SqlParameter("@pictureLocation",item.PictureLocation)});
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
        }

        public static InventoryItem Get_Inventory_Item_Info(int itemID)
        {
            InventoryItem item;
            SqlParameter[] param;
            DataTable dt;

            try
            {
                param = new SqlParameter[1];
                param[1] = (new SqlParameter("@itemID", itemID));
                dt = DataAccess.Get_Data(_connectionString, "Get_ItemInfo", System.Data.CommandType.StoredProcedure, param);

                item = new InventoryItem(Convert.ToInt32(dt.Rows[0]["item_id"]),
                                         dt.Rows[0]["item_description"].ToString(),
                                         Convert.ToDouble(dt.Rows[0]["item_cost"]),
                                         Convert.ToDouble(dt.Rows[0]["item_price"]),
                                         Convert.ToInt32(dt.Rows[0]["group_id"]),
                                         Convert.ToBoolean(dt.Rows[0]["item_isActive"]),
                                         dt.Rows[0]["item_pictureLocation"].ToString(),
                                         Convert.ToInt32(dt.Rows[0]["inventory_quantity"]),
                                         Convert.ToDouble(dt.Rows[0]["inventory_cost"]),
                                         Convert.ToDouble(dt.Rows[0]["inventory_revenue"]));

                return item;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
        }

        public static List<InventoryItem> Get_Inventory_Items()
        {
            List<InventoryItem> items = new List<InventoryItem>();
            
            try
            {
                foreach (DataRow dr in DataAccess.Get_Data(_connectionString, "Get_Items", CommandType.StoredProcedure, null).Rows)
                {
                    items.Add(new InventoryItem(Convert.ToInt32(dr["item_id"]),
                                         dr["item_description"].ToString(),
                                         Convert.ToDouble(dr["item_cost"]),
                                         Convert.ToDouble(dr["item_price"]),
                                         Convert.ToInt32(dr["group_id"]),
                                         Convert.ToBoolean(dr["item_isActive"]),
                                         dr["item_pictureLocation"].ToString(),
                                         Convert.ToInt32(dr["inventory_quantity"]),
                                         Convert.ToDouble(dr["inventory_cost"]),
                                         Convert.ToDouble(dr["inventory_revenue"])));
                }
                return items;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message,ex.InnerException);
            }
        }

        public static int Insert_Transaction(Transaction transaction)
        {
            try
            {
                return Convert.ToInt32(DataAccess.Insert_Data(_connectionString, "Insert_Transaction", CommandType.StoredProcedure,
                                   new SqlParameter[] {new SqlParameter("@itemID",transaction.ItemID),
                                                       new SqlParameter("@transactionDescription",transaction.Description),
                                                       new SqlParameter("@transactionDate",transaction.TransactionDate),
                                                       new SqlParameter("@transactionQuantity",transaction.Quantity),
                                                       new SqlParameter("@transactionAmount",transaction.Amount)}));
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message,ex.InnerException);    
            }
        }

        public static void Update_Transaction(Transaction transaction)
        {
            try
            {
                DataAccess.Send_Data(_connectionString, "Update_Transaction", CommandType.StoredProcedure,
                                   new SqlParameter[] {new SqlParameter("@transactionID",transaction.TransactionID),
                                                       new SqlParameter("@transactionDescription",transaction.Description),
                                                       new SqlParameter("@transactionDate",transaction.TransactionDate),
                                                       new SqlParameter("@transactionQuantity",transaction.Quantity),
                                                       new SqlParameter("@transactionAmount",transaction.Amount)});
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message,ex.InnerException);    
            }
        }

        public static List<Transaction> Get_Transactions(int itemID)
        {
            List<Transaction> transactions = new List<Transaction>();

            try
            {
                foreach (DataRow dr in DataAccess.Get_Data(_connectionString, "Get_Transactions", CommandType.StoredProcedure, new SqlParameter[] { new SqlParameter("@itemID", itemID) }).Rows)
                {
                    transactions.Add(new Transaction(itemID,
                                                     Convert.ToInt32(dr["transaction_id"]),
                                                     dr["transaction_description"].ToString(),
                                                     Convert.ToDateTime(dr["transaction_date"]),
                                                     Convert.ToInt32(dr["transaction_quantity"]),
                                                     Convert.ToDouble(dr["transaction_amount"])));
                }
                return transactions;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
        }
    }
}
